package com.dcjt.firehotcarpad.moudle

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import com.amap.api.maps.model.LatLng
import com.amap.api.navi.AMapNavi
import com.amap.api.navi.AMapNaviIndependentRouteListener
import com.amap.api.navi.AMapNaviViewOptions
import com.amap.api.navi.enums.NaviType
import com.amap.api.navi.model.AMapCalcRouteResult
import com.amap.api.navi.model.AMapNaviPathGroup
import com.amap.api.navi.model.NaviLatLng
import com.amap.api.navi.model.NaviPoi
import com.aries.library.fast.manager.LoggerManager
import com.aries.library.fast.util.SPUtil
import com.aries.library.fast.util.ToastUtil
import com.aries.ui.view.title.TitleBarView
import com.dcjt.firehotcarpad.R
import com.dcjt.firehotcarpad.base.App
import com.dcjt.firehotcarpad.base.SPConstant
import com.dcjt.firehotcarpad.utils.Converter
import kotlinx.android.synthetic.main.activity_nav.*

/**
 * Created by cj on 2021/2/23.
 * Email:codesexy@163.com
 * Function:
 * desc:处理项目中的导航业务
 */
class NavActivity : BaseNaviActivity(), AMapNaviIndependentRouteListener {

    private lateinit var endLatLng: LatLng
    private var toPint: Boolean = false

    companion object {
        // 如果开始导航之前需要使用的选择规划的路径  toPint = false
        fun startNavActivity(
            context: Context,
            endLat: Double,
            endLon: Double,
            toPint: Boolean = true
        ) {
            context.startActivity(Intent(context, NavActivity::class.java).apply {
                putExtra("lat", endLat)
                putExtra("lon", endLon)
                putExtra("toPint", toPint)
            })
        }
    }

    override fun getContentLayout(): Int = R.layout.activity_nav

    override fun setTitleBar(titleBar: TitleBarView?) {
        titleBar?.visibility = View.GONE
    }

    override fun initView(savedInstanceState: Bundle?) {
        mAMapNaviView = navView

        mAMapNaviView!!.onCreate(savedInstanceState)
        mAMapNaviView!!.setAMapNaviViewListener(this)
        val options = AMapNaviViewOptions()
        options.isAutoLockCar = true
        mAMapNaviView!!.viewOptions = options

//        endLatLng = LatLng(intent.getDoubleExtra("lat", 0.0), intent.getDoubleExtra("lon", 0.0))
        //经纬度转换
         endLatLng = Converter.toGCJ02Point_latlon(
             intent.getDoubleExtra("lat", 0.0),
             intent.getDoubleExtra("lon", 0.0)
        )

        toPint = intent.getBooleanExtra("toPint", false)

        startNav()
    }

    /**
     * 开始导航
     * 1. 如果前边已经选路了 使用选择的路径开始导航
     * 2. 如果没有选择路径  算路之后进行导航
     */
    private fun startNav() {
        var navGroup = App.navPathGroup
        if (!toPint && navGroup != null) {
            mAMapNavi.stopNavi()
            mAMapNavi.startNaviWithPath(NaviType.GPS, navGroup)
        } else {
            if (endLatLng.latitude * endLatLng.longitude == 0.0) {
                ToastUtil.show("未传经纬度")
            } else {
                calculatePathAndNav()
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        App.navPathGroup = null // 缓存的这个东西就能取消了
    }

    private fun calculatePathAndNav() {
        val endPoint = NaviPoi("终点", endLatLng, "")
//        val endPoint = NaviLatLng(endLatLng.latitude,endLatLng.longitude)

        val smart = SPUtil.get(this, SPConstant.TAG_SMART_RECOM, true) as Boolean
        val noHigh = SPUtil.get(this, SPConstant.TAG_NO_HIGH_SPEED, false) as Boolean
        val avoidCharge = SPUtil.get(this, SPConstant.TAG_AVOID_CHARGES, false) as Boolean
        val avoidCongest = SPUtil.get(this, SPConstant.TAG_AVOID_CONGESTION, false) as Boolean
        var strategy = if (smart) {
            mAMapNavi.strategyConvert(true, false, true, true, true)
        } else {
            mAMapNavi.strategyConvert(avoidCongest, noHigh, avoidCharge, false, false)
        }

        mAMapNavi.independentCalculateRoute(null, endPoint, null, strategy, 1, this)
//        mAMapNavi.calculateDriveRoute(arrayListOf(endPoint), null, strategy)

    }


    override fun onIndependentCalculateSuccess(pathGroup: AMapNaviPathGroup) {
        pathGroup.selectRouteWithIndex(0)
        mAMapNavi.startNaviWithPath(NaviType.GPS, pathGroup)
    }

    override fun onIndependentCalculateFail(result: AMapCalcRouteResult) {
        ToastUtil.show("导航规划出现错误")
        LoggerManager.e("NavActivity", "${result.errorCode} -- ${result.errorDetail}")
    }

    override fun onCalculateRouteSuccess(aMapCalcRouteResult: AMapCalcRouteResult) {
        super.onCalculateRouteSuccess(aMapCalcRouteResult)
        AMapNavi.getInstance(this).startNavi(NaviType.GPS);
    }


}